#pragma once

#include "templatevector.h"

template <class KEY, class VALUE>
class TemplateTable : public TemplateVector<VALUE>
{
private:
	TemplateVector<KEY> m_keys;
public:
	int keyIndex(const KEY & a_key)
	{
		return m_keys.indexOfWithBinarySearch(a_key);
	}

	VALUE * getByKey(const KEY & a_key)
	{
		int index = m_keys.indexOfWithBinarySearch(a_key);
		return (index < 0)?0:&get(index);
	}

	KEY getKey(const int & a_index)
	{
		return m_keys.get(a_index);
	}

	void replaceKeyWith(const int & a_keyIndex, const KEY & a_newKey)
	{
		m_keys.set(a_keyIndex, a_newKey);
	}

	void add(const KEY & a_key, const VALUE & a_value)
	{
		int index = m_keys.indexOf(a_key);
		if(index < 0)
		{
			// insertion sort
			for(index = 0; index < size() && m_keys.get(index) < a_key; ++index);
			m_keys.insert(index, a_key);
			insert(index, a_value);
		}
		else
		{
			set(index, a_value);
		}
	}

	void release()
	{
		m_keys.release();
		TemplateVector<VALUE>::release();
	}
};